#!/usr/bin/env python
#coding=utf8
"""
@描述：根据数据库中的51三级职能与给定的json文件对比
       得出匹配/不匹配的数量
@日期：2015年02月03日 星期一

@作者：hongxingfan
@教训：json装载的是看标准的文件如[{},{},...]或者字符串"{'aa':21}",其中不能有特殊字符\n\u等
       装载到内存中的相同数据(汉字),可能因为编码问题对应不上,需要留意`
"""

import json
import sys
import re

# 将mysql包文件添加到系统路径中
sys.path.append("/home/wenxiubi/project/ScriptsCode/py_code/MySQL/")
reload(sys)
sys.setdefaultencoding("utf8")
from DBArgs import *
from DBUtils import *

if __name__ == "__main__":
    
    # 数据库操作,查出51三级职能与我们自己的表的对照关系
    select = """
            SELECT NAME,j.thirdName FROM jobs.otherSiteToKanZhun o
            LEFT JOIN jobs.jobclass_tag j 
            ON o.thirdId=j.thirdId
            WHERE o.siteName='job51'
        """
    DB_251 = MySQLDB(DB251.host, DB251.user, DB251.passwd, DB251.port, DB251.charset) 
    selectResult = DB_251.select(select,[])
    contrastMap = dict()
    for(k,v) in selectResult:
        k = k.strip()
        v = v.strip()
        contrastMap[k] = v
    DB_251.close()
    #for(k) in contrastSet:
    #    print("%s" %(k))
    #sys.exit(0)
    # 读取json文件，文件很大是否需要分割;这里并不是标准json格式的文件
    #handler = file(r"/cygdrive/d/__tmp_results.json")
    pattern = re.compile(r"^\{\"jobclass\":\"(.*?)\"")
    handler = open(r"/cygdrive/d/__tmp_results.json")
    totalYes = 0
    totalNo = 0
    total = 0
    mapNo = dict()
    mapYes = dict()
    for line in handler:
        total += 1
        p = pattern.match(line)
        if(p):
            jobclass = p.group(1)
            jobclass = jobclass.replace("\"","").replace("\]","").replace("\[","")
            jobclass = re.split(",|  ",jobclass)
            for j in jobclass:
                # 编码问题整了3个小时，奶奶的
                j = j.strip().decode("utf8")
                if(j in contrastMap):
                    if(j not in mapYes):
                        mapYes[j] = 0
                    mapYes[j] += 1
                    totalYes += 1
                else:
                    if(j not in mapNo):
                        mapNo[j] = 0
                    mapNo[j] += 1
                    totalNo += 1
    print("总条数: %d\t匹配上的条数:%d\t没有匹配上的条数:%d" %(total,totalYes,totalNo))
    for (k,v) in mapNo.items():
        print("%s\t%d" %(k,v))
    print("----------------------------------------------------------------")
    for (k,v) in mapYes.items():
        print("%s\t%d" %(k,v))
